﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VisualizationWindow.xaml.cs" company="TomCorp">
//   Tom
// </copyright>
// <summary>
//   Interaction logic for VisualizationWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace GeoCoder {    
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using System.Net;
    using System.Windows;    
    using GMap.NET;
    using GMap.NET.MapProviders;
    using GMap.NET.WindowsPresentation;
    using Model;

    /// <summary>
    /// Interaction logic for VisualizationWindow.xaml
    /// </summary>
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
    public partial class VisualizationWindow : Window {
        /// <summary>
        /// The herold data.
        /// </summary>
        private readonly List<HeroldData> heroldData = new List<HeroldData>();

        /// <summary>
        /// Initializes a new instance of the <see cref="VisualizationWindow"/> class.
        /// </summary>
        /// <param name="title">
        /// The window Title
        /// </param>
        /// <param name="heroldData">
        /// The herold Data
        /// </param>
        /// <param name="webProxy">
        /// The web Proxy.
        /// </param>
        public VisualizationWindow(string title, List<HeroldData> heroldData, WebProxy webProxy = null) {
            this.InitializeComponent();
            this.heroldData = heroldData.Where(h => h.Latitude != 0 && h.Longitude != 0).ToList();
            this.Title = title;
            
            // config map
            if (webProxy != null) GMapProvider.WebProxy = webProxy;
            GMapControl1.MapProvider = GMapProviders.GoogleMap;
            GMapControl1.Manager.Mode = AccessMode.ServerAndCache;
            GMapControl1.ShowCenter = false;

            // get map types
            comboBoxMapType.ItemsSource = GMapProviders.List;
            comboBoxMapType.DisplayMemberPath = "Name";
            comboBoxMapType.SelectedItem = GMapControl1.MapProvider;
                        
            foreach (var data in this.heroldData) {
                var tooltip = data.DisplayName + " " + Environment.NewLine + data.Street + ", " + data.City;
                var markerShape = new Markers.Marker { ToolTip = tooltip };
                var marker = new GMapMarker(new PointLatLng(data.Latitude, data.Longitude)) { Shape = markerShape };
                GMapControl1.Markers.Add(marker);
            }

            GMapControl1.ZoomAndCenterMarkers(null);
        }

        /// <summary>
        /// The window_ closed_1.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Window_Closed_1(object sender, EventArgs e) {
            // abort all caching threads
            GMapControl1.Manager.CancelTileCaching();
        }
    }
}
